﻿using LD.Model;
using LD.PLC;
using LD.Tool;
using System;
using System.Linq;
using System.Windows.Forms;

namespace LD.Host.项目
{
    public partial class FrmKazamScrew : BaseFrm
    {
        private string barId = Guid.NewGuid().ToString("N");
        private string slotId = Guid.NewGuid().ToString("N");
        private string kazamId = Guid.NewGuid().ToString("N");
        private ModbusCom screw1;
        private ModbusCom screw2;
        public FrmKazamScrew()
        {
            InitializeComponent();
            this.FormClosing += FrmKazamPress_FormClosing;
            this.Shown += FrmKazamPress_Shown;
        }

        private void FrmKazamPress_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void FrmKazamPress_FormClosing(object sender, FormClosingEventArgs e)
        {
            PubSub.Hub.Default.Unsubscribe<BarcodeHandle>(barId);
            PubSub.Hub.Default.Unsubscribe<ScrewHandle>(slotId);
            PubSub.Hub.Default.Unsubscribe<KazamHandle>(kazamId);
        }

        private void Init()
        {
            ControlInit();
            ScrewInit();

            PubSub.Hub.Default.Subscribe<BarcodeHandle>(barId, Slot_Barcode);
            PubSub.Hub.Default.Subscribe<ScrewHandle>(slotId, Screw_Slot);
            PubSub.Hub.Default.Subscribe<KazamHandle>(kazamId, Kazam_Slot);

            KazamService.Ins.Start();

            Start();
        }
        private void ControlInit()
        {
            string slot1 = Cache.GetCache(CONST.HYFSK_SCREW_SLOT1) == null ? "slot1" : Cache.GetCache(CONST.HYFSK_SCREW_SLOT1).ToString();
            string slot2 = Cache.GetCache(CONST.HYFSK_SCREW_SLOT2) == null ? "slot2" : Cache.GetCache(CONST.HYFSK_SCREW_SLOT2).ToString();

            this.slot1.Slot = slot1;
            this.slot2.Slot = slot2;
            this.slot1.Name = slot1;
            this.slot2.Name = slot2;
        }
        private void ScrewInit()
        {
            //暂时写死，左电批为com3,右电批为com1
            //TB_HD hd1 = new TB_HD()
            //{
            //    Address = "COM3",
            //    Name = "左电批",
            //    Port = 115200,
            //    TimeOut = 1000,
            //    Parity = 0
            //};
            //TB_HD hd2 = new TB_HD()
            //{
            //    Address = "COM1",
            //    Name = "右电批",
            //    Port = 115200,
            //    TimeOut = 1000,
            //    Parity = 0
            //};
            //screw1 = new ModbusCom();
            //screw2 = new ModbusCom();
            //screw1.HDParam = hd1;
            //screw2.HDParam = hd2;
            //screw1.Start();
            //screw2.Start();
        }
        public void Slot_Barcode(BarcodeHandle bar)
        {
            this.BeginInvoke(new Action(() =>
            {
                var ctl = getSlot("扫码" + bar.Slot.Replace("Slot", ""));
                if (ctl == null)
                    return;

                ctl.Barcode = bar.Barcode;
                ctl.StateMsg = string.IsNullOrEmpty(bar.Barcode) ? "扫码失败" : "扫码成功";
                ctl.State = string.IsNullOrEmpty(bar.Barcode) ? KazamState.Error : KazamState.Start;
            }));
        }
        public void Kazam_Slot(KazamHandle kaz)
        {
            this.BeginInvoke(new Action(() =>
            {
                var ctl = getSlot(kaz.Slot);
                if (ctl == null)
                    return;

                ctl.StateMsg = kaz.message;
                ctl.State = kaz.kazamState;
            }));
        }
        public void Screw_Slot(ScrewHandle kaz)
        {
            this.BeginInvoke(new Action(() =>
            {
                var ctl = getSlot("Slot" + kaz.Slot);
                if (ctl == null)
                    return;

                ctl.StateMsg = kaz.Message;
                ctl.State = kaz.KazamState;
                ctl.Barcode = kaz.Barcode;

                var ctlIo = getCtlIo("Slot" + kaz.Slot, kaz.Num);
                if (ctlIo == null)
                    return;

                ctlIo.Num = kaz.Num;

                if (kaz.Num == 1)
                {
                    InitIo("Slot" + kaz.Slot);
                }
                switch (kaz.KazamState)
                {
                    case KazamState.Start:
                        ctlIo.LevelType = ScrewEnum.Start;
                        break;
                    case KazamState.Testing:
                        ctlIo.LevelType = ScrewEnum.Testing;
                        break;
                    case KazamState.Complated:
                        ctlIo.LevelType = ScrewEnum.Complated;
                        break;
                    case KazamState.Error:
                        ctlIo.LevelType = ScrewEnum.Error;
                        break;
                    default:
                        break;
                }
            }));
        }
        private void InitIo(string slot)
        {
            for (int i = 1; i < 9; i++)
            {
                var ctlIo = getCtlIo(slot, i);
                if (ctlIo == null)
                    return;
                ctlIo.LevelType = ScrewEnum.Start;
            }
        }
        private CtlSLOT getSlot(string slot)
        {
            if (slot == null)
                return null;
            var ctl = this.Controls.Find(slot, false).FirstOrDefault();
            if (ctl == null)
                return null;
            else
                return (CtlSLOT)ctl;
        }
        private CtlIO getCtlIo(string slot, int num)
        {
            if (slot == null)
                return null;
            var ctl = this.Controls.Find(slot + num, false).FirstOrDefault();
            if (ctl == null)
                return null;
            else
                return (CtlIO)ctl;
        }
        private void Start()
        {


        }
    }
}
